//
//  JSCountrySelectView.m
//  NiubilityProject
//
//  Created by Apple on 2017/11/20.
//  Copyright © 2017年 Apple. All rights reserved.
//

#import "JSCountrySelectView.h"
#import "JSSeetingUserInfoVC.h"
#import "JSCountryInfoModel.h"
@interface JSCountrySelectView()<UIPickerViewDelegate,UIPickerViewDataSource>
/** dataSource*/
@property (nonatomic, strong) NSArray *dataSource;
@property (nonatomic, weak) JSCountryInfoModel *m;
@property (nonatomic, strong) UIPickerView *pickerView;;

@end

@implementation JSCountrySelectView

- (instancetype)initWithFrame:(CGRect)frame dataSource:(NSArray *)dataSource{
    if (self = [super initWithFrame:frame]) {
        
        self.dataSource = dataSource;
        self.backgroundColor = [UIColor whiteColor];
        JS_WeakSelf(self)
        // 1.顶部视图
        UIView *topView = [UIView new];
        topView.backgroundColor = JS_colorWithHex(kBodyColor);
        [self addSubview:topView];
        [topView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.and.left.and.right.equalTo(weakself);
            make.height.mas_equalTo(40);
        }];
        // 1.1 关闭按钮
        UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [closeBtn addTarget:self action:@selector(hidenMaskView) forControlEvents:UIControlEventTouchUpInside];
        [closeBtn setImage:[UIImage qmui_imageWithShape:QMUIImageShapeNavClose size:CGSizeMake(15, 15) tintColor:[UIColor whiteColor]] forState:UIControlStateNormal];
        [topView addSubview:closeBtn];
        closeBtn.frame = CGRectMake(0, 0, 40, 40);
//        [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.top.and.left.and.bottom.equalTo(topView);
//            make.width.mas_equalTo(kRealValue(80));
//        }];
        // 1.2 完成
        UIButton *finishBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [finishBtn addTarget:self action:@selector(selectFinish:) forControlEvents:UIControlEventTouchUpInside];
        finishBtn.titleLabel.font = UIFontMake(15);
        [finishBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [finishBtn setTitle:JSLocalizedString(kLocalizableFinish) forState:UIControlStateNormal];
        [topView addSubview:finishBtn];
        [finishBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.and.right.and.bottom.equalTo(topView);
             make.width.mas_equalTo(kRealValue(80));
        }];
        // 1.3 描述
        UILabel *desL = [UILabel new];
        desL.textColor = [UIColor whiteColor];
        desL.font = UIFontMake(14);
        desL.textAlignment = NSTextAlignmentCenter;
        desL.text = JSLocalizedString(@"countryLoca");
        [topView addSubview:desL];
        desL.text = JSLocalizedString(@"countryLoca");
        [desL sizeToFit];
        [desL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(topView);
        }];
        // 2 picker view
        _pickerView = [UIPickerView new];
        [self addSubview:_pickerView];
        _pickerView.dataSource = self;
        _pickerView.delegate   = self;
        [_pickerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.and.right.and.bottom.equalTo(weakself);
            make.top.equalTo(topView.mas_bottom);
        }];
        // 0.数据初始
        [self pickerView:_pickerView didSelectRow:0 inComponent:0];
    }
    return self;
}
// 返回多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
    
}
// 返回多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return self.dataSource.count;
    
}
// 返回每行的标题
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    JSCountryInfoModel *m = self.dataSource[row];
    if (JSLocalizeIsChina()) {
         return m.country_zh;
    }
    return m.country;
}
// 选中行显示在label上
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    JSLog(@"%zd--%zd",row,component);
    self.m = self.dataSource[row];
    
    if (JSLocalizeIsChina()) {
        self.selectCountryStr = self.m.country_zh;
    }else{
        self.selectCountryStr = self.m.country;
    }
}
-(void)selectFinish:(UIButton *)sender {
    if ([self.delegate respondsToSelector:@selector(didFinishCountry:countryM:)]) {
        [self.delegate didFinishCountry:self.selectCountryStr countryM:self.m];
    }
    [self hidenMaskView];
}

@end
